home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / dde1 / main.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1993-05-26  |  16.6 KB  |  551 lines

  1. VERSION 2.00
  2. Begin Form frmMain 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "DDE Experimenter"
  5.    FontTransparent =   0   'False
  6.    Height          =   5745
  7.    Left            =   930
  8.    LinkMode        =   1  'Source
  9.    LinkTopic       =   "System"
  10.    ScaleHeight     =   5340
  11.    ScaleWidth      =   6210
  12.    Top             =   1125
  13.    Width           =   6330
  14.    Begin Frame Frames 
  15.       Caption         =   "Destination Data"
  16.       Height          =   3015
  17.       Index           =   2
  18.       Left            =   120
  19.       TabIndex        =   21
  20.       Top             =   2280
  21.       Width           =   6015
  22.       Begin TextBox txtData 
  23.          Height          =   2160
  24.          Left            =   120
  25.          MultiLine       =   -1  'True
  26.          ScrollBars      =   3  'Both
  27.          TabIndex        =   22
  28.          Text            =   "Text1"
  29.          Top             =   720
  30.          Width           =   5760
  31.       End
  32.       Begin OptionButton optDataType 
  33.          Caption         =   "Graphics"
  34.          Height          =   255
  35.          Index           =   1
  36.          Left            =   1440
  37.          TabIndex        =   14
  38.          Top             =   360
  39.          Width           =   1815
  40.       End
  41.       Begin OptionButton optDataType 
  42.          Caption         =   "Text"
  43.          Height          =   255
  44.          Index           =   0
  45.          Left            =   120
  46.          TabIndex        =   13
  47.          Top             =   360
  48.          Value           =   -1  'True
  49.          Width           =   1095
  50.       End
  51.       Begin PictureBox picData 
  52.          AutoRedraw      =   -1  'True
  53.          DrawWidth       =   2
  54.          Height          =   2160
  55.          Left            =   120
  56.          ScaleHeight     =   2130
  57.          ScaleWidth      =   5730
  58.          TabIndex        =   23
  59.          Top             =   720
  60.          Visible         =   0   'False
  61.          Width           =   5760
  62.       End
  63.    End
  64.    Begin Frame Frames 
  65.       Caption         =   "Source Properties"
  66.       Height          =   1440
  67.       Index           =   1
  68.       Left            =   4200
  69.       TabIndex        =   20
  70.       Top             =   720
  71.       Width           =   1920
  72.       Begin TextBox txtSourceTopic 
  73.          Height          =   285
  74.          Left            =   120
  75.          TabIndex        =   17
  76.          Text            =   "System"
  77.          Top             =   960
  78.          Width           =   1695
  79.       End
  80.       Begin CheckBox chkSourceMode 
  81.          Caption         =   "&Source Enabled"
  82.          Height          =   240
  83.          Left            =   120
  84.          TabIndex        =   15
  85.          Top             =   360
  86.          Value           =   1  'Checked
  87.          Width           =   1680
  88.       End
  89.       Begin Label Labels 
  90.          Caption         =   "Source &Link Topic"
  91.          Height          =   240
  92.          Index           =   3
  93.          Left            =   120
  94.          TabIndex        =   16
  95.          Top             =   720
  96.          Width           =   1680
  97.       End
  98.    End
  99.    Begin CommandButton cmdExit 
  100.       Caption         =   "E&xit"
  101.       Height          =   480
  102.       Left            =   4200
  103.       TabIndex        =   18
  104.       Top             =   120
  105.       Width           =   1920
  106.    End
  107.    Begin Frame Frames 
  108.       Caption         =   "Destination Properties"
  109.       Height          =   2160
  110.       Index           =   0
  111.       Left            =   120
  112.       TabIndex        =   19
  113.       Top             =   0
  114.       Width           =   3960
  115.       Begin ComboBox cboAppName 
  116.          Height          =   300
  117.          Left            =   1200
  118.          TabIndex        =   1
  119.          Text            =   "ProgMan"
  120.          Top             =   360
  121.          Width           =   1215
  122.       End
  123.       Begin ComboBox cboTopic 
  124.          Height          =   300
  125.          Left            =   720
  126.          TabIndex        =   3
  127.          Text            =   "ProgMan"
  128.          Top             =   720
  129.          Width           =   1695
  130.       End
  131.       Begin ComboBox cboItem 
  132.          Height          =   300
  133.          Left            =   720
  134.          TabIndex        =   5
  135.          Top             =   1080
  136.          Width           =   1695
  137.       End
  138.       Begin OptionButton optLinkMode 
  139.          Caption         =   "&Notify"
  140.          Height          =   240
  141.          Index           =   3
  142.          Left            =   2640
  143.          TabIndex        =   8
  144.          Top             =   1200
  145.          Width           =   960
  146.       End
  147.       Begin CommandButton cmdExecute 
  148.          Caption         =   "&Execute"
  149.          Enabled         =   0   'False
  150.          Height          =   480
  151.          Left            =   2640
  152.          TabIndex        =   12
  153.          Top             =   1560
  154.          Width           =   1080
  155.       End
  156.       Begin CommandButton cmdPoke 
  157.          Caption         =   "&Poke"
  158.          Enabled         =   0   'False
  159.          Height          =   480
  160.          Left            =   1440
  161.          TabIndex        =   11
  162.          Top             =   1560
  163.          Width           =   1080
  164.       End
  165.       Begin CommandButton cmdRequest 
  166.          Caption         =   "&Request"
  167.          Enabled         =   0   'False
  168.          Height          =   480
  169.          Left            =   240
  170.          TabIndex        =   10
  171.          Top             =   1560
  172.          Width           =   1080
  173.       End
  174.       Begin OptionButton optLinkMode 
  175.          Caption         =   "&Manual"
  176.          Height          =   240
  177.          Index           =   2
  178.          Left            =   2640
  179.          TabIndex        =   7
  180.          Top             =   960
  181.          Width           =   960
  182.       End
  183.       Begin OptionButton optLinkMode 
  184.          Caption         =   "A&utomatic"
  185.          Height          =   240
  186.          Index           =   1
  187.          Left            =   2640
  188.          TabIndex        =   6
  189.          Top             =   720
  190.          Width           =   1200
  191.       End
  192.       Begin CommandButton cmdConnect 
  193.          Caption         =   "&Connect"
  194.          Height          =   480
  195.          Left            =   2520
  196.          TabIndex        =   9
  197.          Top             =   240
  198.          Width           =   1320
  199.       End
  200.       Begin Label Labels 
  201.          Caption         =   "Item"
  202.          Height          =   255
  203.          Index           =   2
  204.          Left            =   120
  205.          TabIndex        =   4
  206.          Top             =   1080
  207.          Width           =   615
  208.       End
  209.       Begin Label Labels 
  210.          Caption         =   "&Topic"
  211.          Height          =   255
  212.          Index           =   1
  213.          Left            =   120
  214.          TabIndex        =   2
  215.          Top             =   720
  216.          Width           =   615
  217.       End
  218.       Begin Label Labels 
  219.          Caption         =   "&Application"
  220.          Height          =   255
  221.          Index           =   0
  222.          Left            =   120
  223.          TabIndex        =   0
  224.          Top             =   360
  225.          Width           =   975
  226.       End
  227.    End
  228.    Begin Label lblSysLink 
  229.       Height          =   375
  230.       Left            =   4440
  231.       TabIndex        =   25
  232.       Top             =   5400
  233.       Visible         =   0   'False
  234.       Width           =   1455
  235.    End
  236.    Begin Label Topics 
  237.       Height          =   375
  238.       Left            =   120
  239.       TabIndex        =   24
  240.       Top             =   5400
  241.       Visible         =   0   'False
  242.       Width           =   2175
  243.    End
  244. Option Explicit
  245. Option Compare Text     ' Perform case-insensitive string comparisons
  246. Dim TopicChangeFlag As Integer, appChangeFlag As Integer, Connected As Integer
  247. Dim NotifyFlag As Integer
  248. Const DEST_TEXT = 0, DEST_PIC = 1
  249. Const MNU_COPY = 0, MNU_PASTE = 1, MNU_PASTELINK = 2
  250. Sub cboAppName_Click ()
  251.     If Connected Then cmdConnect.Value = True
  252.     FillTopicList
  253. End Sub
  254. Sub cboAppName_LostFocus ()
  255.     If appChangeFlag Then
  256.     appChangeFlag = False
  257.     If Connected Then cmdConnect.Value = True
  258.     FillTopicList
  259.     End If
  260. End Sub
  261. Sub cboItem_Change ()
  262. On Error Resume Next
  263.     picData.LinkItem = cboItem.Text
  264.     txtData.LinkItem = cboItem.Text
  265. End Sub
  266. Sub cboItem_Click ()
  267.     picData.LinkItem = cboItem.Text
  268.     txtData.LinkItem = cboItem.Text
  269. End Sub
  270. Sub cboTopic_Change ()
  271.     TopicChangeFlag = True
  272.     CheckForSystemTopic
  273. End Sub
  274. Sub cboTopic_Click ()
  275.     If Connected Then cmdConnect.Value = True
  276.     CheckForSystemTopic
  277. End Sub
  278. Sub cboTopic_LostFocus ()
  279.     If TopicChangeFlag Then
  280.     TopicChangeFlag = False
  281.     If Connected Then cmdConnect.Value = True
  282.     CheckForSystemTopic
  283.     End If
  284. End Sub
  285. Sub ChangeLinkTopic ()
  286. End Sub
  287. Sub CheckForSystemTopic ()
  288. Dim i
  289.     If cboTopic.Text = "SYSTEM" Or cboTopic.Text = "PROGMAN" Then
  290.     FillSysItems
  291.     optLinkMode(1).Enabled = False
  292.     optLinkMode(3).Enabled = False
  293.     optLinkMode(2).Value = True
  294.     Else
  295.     For i = 1 To 3
  296.         optLinkMode(i).Enabled = True
  297.     Next
  298.     cboItem.Clear
  299.     cboItem.Text = ""
  300.     If cboAppName.Text = "WINWORD" Then
  301.         cboItem.AddItem "\Doc"
  302.         cboItem.Text = "\Doc"
  303.         cboItem.Refresh
  304.     End If
  305.     End If
  306. End Sub
  307. Sub chkSourceMode_Click ()
  308.     LinkMode = Abs(chkSourceMode.Value)
  309.     txtSourceTopic.Enabled = chkSourceMode.Value
  310. End Sub
  311. Sub cmdConnect_Click ()
  312. Dim clientLinkMode As Integer
  313.     If Not Connected Then
  314.     For clientLinkMode = 1 To 3
  315.         If optLinkMode(clientLinkMode).Value Then Exit For
  316.     Next
  317.     picData.Picture = LoadPicture()
  318.     txtData.Text = ""
  319.     Select Case MakeConnection(clientLinkMode)
  320.         Case 0
  321.         ConnectState True
  322.         Case NO_APP_RESPONDED
  323.         If MsgBox("Hey! " & cboAppName.Text & " doesn't seem to be running. Should I start it?", MB_YESNO + MB_ICONQUESTION) = IDYES Then
  324.             If StartApp((cboAppName.Text)) Then
  325.             Select Case MakeConnection(clientLinkMode)
  326.                 Case 0
  327.                 ConnectState True
  328.                 Case NO_APP_RESPONDED
  329.                 MsgBox "Sorry, still can't connect."
  330.             End Select
  331.             End If
  332.         End If
  333.     End Select
  334.     Else
  335.     Disconnect txtData
  336.     Disconnect picData
  337.     ConnectState False
  338.     End If
  339. End Sub
  340. Sub CmdExecute_Click ()
  341.     ' Empty combo box on Execute form
  342.     ' (This also implictly loads the form if it was unloaded).
  343.     frmExecute.cboExecuteString.Clear
  344.     ' Load sample execute strings appropriate to the source application
  345.     Select Case cboAppName.Text
  346.     Case "ProgMan"
  347.         frmExecute.cboExecuteString.AddItem "[CreateGroup(DDE Group)]"
  348.         frmExecute.cboExecuteString.AddItem "[AddItem(C:\VB\SAMPLES\DDE.EXE, Visual Basic DDE App)]"
  349.         frmExecute.cboExecuteString.AddItem "[ShowGroup(DDE Group, 7)]"
  350.     Case "Excel"
  351.         frmExecute.cboExecuteString.AddItem "[SELECT(" & Chr(34) & "R1:R16384" & Chr(34) & ")]"
  352.         frmExecute.cboExecuteString.AddItem "[NEW(2,2)]"
  353.         frmExecute.cboExecuteString.AddItem "[GALLERY.3D.PIE(4)]"
  354.         frmExecute.cboExecuteString.AddItem "[CLOSE()]"
  355.     Case "WinWord"
  356.         frmExecute.cboExecuteString.AddItem "[StartOfLine][EndOfLine 1]"
  357.         frmExecute.cboExecuteString.AddItem "[InsertBookmark .Name = " & Chr(34) & "DDE1" & Chr(34) & "]"
  358.         frmExecute.cboExecuteString.AddItem "[LineDown 1]"
  359.     End Select
  360.     frmExecute.Show MODAL
  361. End Sub
  362. Sub cmdExit_Click ()
  363.     Unload frmMain
  364.     End
  365. End Sub
  366. Sub cmdPoke_Click ()
  367. On Error Resume Next
  368.     txtData.LinkPoke
  369.     If Err Then MsgBox Error
  370. End Sub
  371. Sub cmdRequest_Click ()
  372. On Error Resume Next
  373.     txtData.LinkRequest
  374.     picData.LinkRequest
  375.     NotifyFlag = False
  376. End Sub
  377. Sub ConnectState (State As Integer)
  378. Dim i As Integer
  379.     If State Then
  380.     cmdConnect.Caption = "Disconnect"
  381.     Else
  382.     cmdConnect.Caption = "Connect"
  383.     End If
  384.     Connected = State
  385.     cmdRequest.Enabled = State
  386.     cmdPoke.Enabled = (optLinkMode(LINK_MANUAL).Value And State)
  387.     cmdExecute.Enabled = State
  388.     'cboAppName.Enabled = Not State
  389.     'cboTopic.Enabled = Not State
  390. End Sub
  391. Function CreateLink (Ctl As Control, appname As String, topic As String, item As String, LinkType As Integer) As Integer
  392. On Error Resume Next
  393.     Ctl.LinkMode = NONE
  394.     Ctl.LinkTopic = appname & "|" & topic
  395.     Ctl.LinkItem = item
  396.     Ctl.LinkMode = LinkType
  397.     CreateLink = Err
  398.     If Err = 0 And LinkType <> LINK_AUTOMATIC Then
  399.     Ctl.LinkRequest
  400.     End If
  401. End Function
  402. Sub Disconnect (Ctl As Control)
  403. Dim tempTimeOutVal
  404. On Error Resume Next    ' Disconnecting with ProgMan causes timeout error: just eat it and go on.
  405.     tempTimeOutVal = Ctl.LinkTimeout
  406.     Ctl.LinkTimeout = 1
  407.     Ctl.LinkMode = NONE
  408.     Ctl.LinkTimeout = tempTimeOutVal
  409. End Sub
  410. Sub FillList (cbo As Control, lbl As Control)
  411. Dim i As Integer, lasti As Integer
  412.     Do
  413.     i = i + 1
  414.     lasti = i
  415.     i = InStr(lasti, lbl.Caption, Chr(9))
  416.     If i = 0 Then
  417.         cbo.AddItem Mid(lbl.Caption, lasti)
  418.         Exit Do
  419.     Else
  420.         cbo.AddItem Mid(lbl.Caption, lasti, i - lasti)
  421.     End If
  422.     Loop
  423. End Sub
  424. Sub FillSysItems ()
  425.     cboItem.Clear
  426.     Screen.MousePointer = HOURGLASS
  427.     lblSysLink.LinkMode = NONE
  428.     lblSysLink.LinkTopic = cboAppName.Text & "|" & "System"
  429.     lblSysLink.LinkItem = "SysItems"
  430.     On Error Resume Next
  431.     lblSysLink.LinkMode = LINK_MANUAL
  432.     If Err = 0 Then
  433.     lblSysLink.LinkRequest
  434.     FillList cboItem, lblSysLink
  435.     cboItem.Text = "SysItems"
  436.     End If
  437.     cboItem.Refresh
  438.     Screen.MousePointer = DEFAULT
  439. End Sub
  440. Sub FillTopicList ()
  441.     cboTopic.Clear
  442.     cboTopic.Text = ""
  443.     If cboAppName.Text = "ProgMan" Then
  444.     cboTopic.Text = "ProgMan"
  445.     Else
  446.     Screen.MousePointer = HOURGLASS
  447.     lblSysLink.LinkMode = NONE
  448.     lblSysLink.LinkTopic = cboAppName.Text & "|" & "System"
  449.     lblSysLink.LinkItem = "Topics"
  450.     On Error Resume Next
  451.     lblSysLink.LinkMode = LINK_MANUAL
  452.     If Err Then
  453.         cboTopic.AddItem "System"
  454.     Else
  455.         lblSysLink.LinkRequest
  456.         FillList cboTopic, lblSysLink
  457.         cboTopic.Text = "System"
  458.     End If
  459.     Screen.MousePointer = DEFAULT
  460.     End If
  461.     cboTopic.Refresh
  462. End Sub
  463. Sub Form_Load ()
  464.     cboAppName.AddItem "ProgMan"
  465.     cboAppName.AddItem "DDE"
  466.     cboAppName.AddItem "Excel"
  467.     cboAppName.AddItem "WinWord"
  468.     cboAppName.AddItem "FoxPro"
  469.     cboAppName.AddItem "Access"
  470.     cboAppName.AddItem "Project"
  471.     LinkTopic = txtSourceTopic.Text
  472.     Topics.Caption = "Topics" & Chr(9) & "picData" & Chr(9) & "txtData" & Chr(13) & Chr(10)
  473. End Sub
  474. Sub Form_Unload (Cancel As Integer)
  475.     Disconnect txtData
  476.     Disconnect picData
  477. End Sub
  478. Function MakeConnection (clientLinkMode As Integer) As Integer
  479. Dim ConnectTxt As Integer, ConnectPic As Integer
  480.     ConnectPic = CreateLink(picData, (cboAppName.Text), (cboTopic.Text), (cboItem.Text), clientLinkMode)
  481.     ConnectTxt = CreateLink(txtData, (cboAppName.Text), (cboTopic.Text), (cboItem.Text), clientLinkMode)
  482.     If ConnectPic = NO_APP_RESPONDED And ConnectTxt = NO_APP_RESPONDED Then
  483.     MakeConnection = NO_APP_RESPONDED
  484.     ElseIf ConnectTxt = 0 Then
  485.     MakeConnection = 0
  486.     optDataType(DEST_TEXT).Value = True
  487.     ElseIf ConnectPic = 0 Then
  488.     MakeConnection = 0
  489.     optDataType(DEST_PIC).Value = True
  490.     Else
  491.     MakeConnection = ConnectPic
  492.     End If
  493. End Function
  494. Sub optDataType_Click (Index As Integer)
  495.     If Index = DEST_TEXT Then
  496.     txtData.Visible = True
  497.     picData.Visible = False
  498.     ElseIf Index = DEST_PIC Then
  499.     txtData.Visible = False
  500.     picData.Visible = True
  501.     End If
  502. End Sub
  503. Sub optLinkMode_Click (Index As Integer)
  504.     If Connected Then
  505.     cmdConnect.Value = True
  506.     cmdConnect.Value = True
  507.     End If
  508. End Sub
  509. Sub picData_LinkClose ()
  510.     ConnectState False
  511. End Sub
  512. Sub picData_LinkNotify ()
  513.     If Not NotifyFlag Then
  514.     MsgBox "New data is available from the DDE Source.  Choose Request to update."
  515.     NotifyFlag = True
  516.     End If
  517. End Sub
  518. Sub picData_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
  519.     If Button And 1 Then
  520.     PSet (X, Y)
  521.     Else
  522.     picData.ForeColor = QBColor(Rnd * 16)
  523.     End If
  524. End Sub
  525. Sub picData_MouseMove (Button As Integer, Shift As Integer, X As Single, Y As Single)
  526.     If Button And 1 Then picData.Line -(X, Y)
  527. End Sub
  528. Sub picData_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)
  529.     If Button And 1 Then
  530.     picData.LinkSend
  531.     End If
  532. End Sub
  533. Function StartApp (appname As String) As Integer
  534. On Error Resume Next
  535.     StartApp = (Shell(appname) > 31)
  536.     If Err Then MsgBox "Couldn't start " & appname
  537.     StartApp = 0
  538. End Function
  539. Sub txtData_LinkClose ()
  540.     ConnectState False
  541. End Sub
  542. Sub txtData_LinkNotify ()
  543.     If Not NotifyFlag Then
  544.     MsgBox "New data is available from the DDE Source.  Choose Request to update."
  545.     NotifyFlag = True
  546.     End If
  547. End Sub
  548. Sub txtSourceTopic_Change ()
  549.     LinkTopic = txtSourceTopic.Text
  550. End Sub
  551.